﻿(function () {
    $(function () {

        var _$table = $('#tb');
        var _alcCodeDetailService = abp.services.app.alcCodeDetail;

        var _permissions = {
            create: abp.auth.hasPermission("Pages.Bom.AlcCodeDetails.Create"),
            edit: abp.auth.hasPermission("Pages.Bom.AlcCodeDetails.Edit"),
            'delete': abp.auth.hasPermission("Pages.Bom.AlcCodeDetails.Delete")

        };


      var _createOrEditModal = new app.ModalManager({
            viewUrl: abp.appPath + 'AlcCodeDetails/CreateOrEditAlcCodeDetailModal',
            scriptUrl: abp.appPath + 'Views/AlcCodeDetails/_CreateOrEditAlcCodeDetailModal.js',
            modalClass: 'CreateOrEditAlcCodeDetailModal'
        });

    
      var TableInit = function () {
          var oTableInit = new Object();
          oTableInit.Init = function () {
              _$table.bootstrapTable({
                  url: '/Colors/GetAllColors', //请求后台的URL（*）
                  method: 'get', //请求方式（*）
                  toolbar: '#toolbar', //工具按钮用哪个容器
                  striped: true, //是否显示行间隔色
                  cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                  pagination: true, //是否显示分页（*）
                  sortable: true, //是否启用排序
                  sortOrder: "asc", //排序方式
                  queryParams: oTableInit.queryParams, //传递参数（*）
                  sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
                  pageNumber: 1, //初始化加载第一页，默认第一页
                  pageSize: 15, //每页的记录行数（*）
                  pageList: [10, 25, 50, 100], //可供选择的每页的行数（*）
                  search: false, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                  strictSearch: true,
                  showColumns: true, //是否显示所有的列
                  showRefresh: true, //是否显示刷新按钮
                  minimumCountColumns: 2, //最少允许的列数
                  clickToSelect: true, //是否启用点击选中行
                  //height: 500, //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                  uniqueId: "Id", //每一行的唯一标识，一般为主键列
                  showToggle: true, //是否显示详细视图和列表视图的切换按钮
                  cardView: false, //是否显示详细视图
                  detailView: false, //是否显示父子表
                  showExport: true,                     //是否显示导出
                  exportDataType: "all",              //basic', 'all', 'selected'.
                  columns: [{
                      checkbox: true
                  }, {
                      field: 'Id',
                      align: 'center',
                      title: '序号',
                  },
                  {
                      field: 'AlcCodeId',
                      align: 'center',
                      title: app.localize('AlcCodeId')
                  },
                  {
                      field: 'SeatPositionId',
                      align: 'center',
                      title: app.localize('SeatPositionId')
                  },
                  {
                      field: 'BackrestAssemblyNo',
                      align: 'center',
                      title: app.localize('BackrestAssemblyNo')
                  },
                  {
                      field: 'CushionAssemblyNo',
                      align: 'center',
                      title: app.localize('CushionAssemblyNo'),
                  },
                  {
                      field: 'BackrestCoverNo',
                      align: 'center',
                      title: app.localize('BackrestCoverNo'),
                  },
                  {
                      field: 'CushionCoverNo',
                      align: 'center',
                      title: app.localize('CushionCoverNo'),
                  },
                  {
                      field: 'BackrestFoamNo',
                      align: 'center',
                      title: app.localize('BackrestFoamNo'),
                  },
                  {
                      field: 'CushionFoamNo',
                      align: 'center',
                      title: app.localize('CushionFoamNo'),
                  },
                  {
                      field: 'BackFrameNo',
                      align: 'center',
                      title: app.localize('BackFrameNo'),
                  },
                  {
                      field: 'SABNo',
                      align: 'center',
                      title:  app.localize('SABNo'),
                  },
                  {
                      field: 'HeadrestNo',
                      align: 'center',
                      title: app.localize('HeadrestNo'),
                  },
                  {
                      field: 'CenterHeadrestNo',
                      align: 'center',
                      title: app.localize('CenterHeadrestNo'),
                  },
                  {
                      field: 'ArmRestNo',
                      align: 'center',
                      title: app.localize('ArmRestNo'),
                  },
                  {
                      field: 'BuckleNo',
                      align: 'center',
                      title: app.localize('BuckleNo'),
                  },
                  {
                      field: 'BackplaneNo',
                      align: 'center',
                      title: app.localize('BackplaneNo'),
                  },
                  {
                      field: 'AdjusterNo',
                      align: 'center',
                      title: app.localize('AdjusterNo'),
                  },
                  {
                      field: 'AdjusterPlateNo',
                      align: 'center',
                      title: app.localize('AdjusterPlateNo'),
                  },
                  {
                      field: 'CushioSsupportNo',
                      align: 'center',
                      title: app.localize('CushioSsupportNo'),
                  },
                  {
                      field: 'SeatColor',
                      align: 'center',
                      title: app.localize('SeatColor'),
                  },
                  {
                      field: 'PlasticColor',
                      align: 'center',
                      title:  app.localize('PlasticColor'),
                  },
                  {
                      field: 'MaterialId',
                      align: 'center',
                      title: app.localize('MaterialId'),
                  },
                  {
                      field: 'Slide',
                      align: 'center',
                      title: app.localize('Slide'),
                  },
                  {
                      field: 'HGIT',
                      align: 'center',
                      title: app.localize('HGIT'),
                  },
                  {
                      field: 'LumbarSupport',
                      align: 'center',
                      title: app.localize('LumbarSupport'),
                  },
                  {
                      field: 'Warmer',
                      align: 'center',
                      title: app.localize('Warmer'),
                  },
                  {
                      field: 'Ventlation',
                      align: 'center',
                      title: app.localize('Ventlation'),
                  },
                  {
                      field: 'SAB',
                      align: 'center',
                      title: app.localize('SAB'),
                  },
                  {
                      field: 'BackPocket',
                      align: 'center',
                      title:  app.localize('BackPocket'),
                  },
                  {
                      field: 'SBR',
                      align: 'center',
                      title: app.localize('SBR'),
                  },
                  {
                      field: 'IMS',
                      align: 'center',
                      title: app.localize('IMS'),
                  },
                  {
                      field: 'OneTouch',
                      align: 'center',
                      title: app.localize('OneTouch'),
                  },
                  {
                      field: 'CreationTime',
                      align: 'center',
                      title: app.localize('CreationTime'),
                      formatter: function (value, row, index) {
                          var time = row.CreationTime;
                          return moment(time).format("YYYY-MM-DD HH:mm:ss")
                      }
                  }, {
                      field: 'operate',
                      title: app.localize('operate'),
                      align: 'center',
                      valign: 'middle',
                      clickToSelect: false,
                      formatter: operateFormatter,
                      events: operateEvents
                  }],
              });
          };
          //指定操作组
          function operateFormatter(value, row, index) {
              var $span = '';
              $span.add
              if (_permissions.edit) {
                  $span = $span + '<a class="edit" href="javascript:void(0)" title="'
                      + app.localize('Edit') + '"><i class="glyphicon glyphicon-edit"></i></a>'
              }
              //删除
              if (_permissions.delete) {
                  $span = $span + '<a class="remove" href="javascript:void(0)" title="'
                      + app.localize('Delete') + '"><i class="glyphicon glyphicon-remove"></i></a>'
              }
              return $span;
          }

          //指定table表体操作事件
          window.operateEvents = {
              'click .edit': function (e, value, row, index) {
                  _createOrEditModal.open({ id: row.Id });
              },
              'click .remove': function (e, value, row, index) {
                  deleteColor(row);
              }
          };

          //得到查询的参数
          oTableInit.queryParams = function (params) {
              var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                  limit: params.limit,   //页面大小
                  offset: params.offset  //页码
                  //color: $("#txt_search_color").val(),
                  //name: $("#txt_search_name").val(),
                  //issingle: $("#sel_search_issingle").val(),
                  //trend: $("#txt_search_trend").val()
              };
              return temp;
          };
          return oTableInit;
      };

        // #region CRUD按钮操作

      $("#btn-add").click(function () {
          _createOrEditModal.open();
      });

      $("#btn-edit").click(function () {
          var selectedRaido = _$table.bootstrapTable('getSelections');
          if (selectedRaido.length === 0) {
              abp.notify.warn(app.localize('PleaseSelectForEdit'));
          } else {
              _createOrEditModal.open({ id: selectedRaido[0].Id });
          }
      });

      $("#btn-delete").click(function () {
          var selectedRaido = _$table.bootstrapTable('getSelections');
          if (selectedRaido.length === 0) {
              abp.notify.warn(app.localize('PleaseSelectForDelete'));
          } else {
              deleteColor(selectedRaido[0]);
          }
      });
        // #endregion

        //删除当前basicColors实体
      function deleteColor(color) {
          abp.message.confirm(
              app.localize('ColorDeleteWarningMessage', color.ColorName),
                  function (isConfirmed) {
                      if (isConfirmed) {
                          _colorService.deleteColorAsync({
                              id: Color.Id
                          }).done(function () {
                              _$table.bootstrapTable('refresh');
                              abp.notify.success(app.localize('SuccessfullyDeleted'));
                          });
                      }
                  }
              );
      }

        //导出为excel文档
      $('#btn-export').click(function () {
          _colorService
              .getColorsToExcel({})
                  .done(function (result) {
                      app.downloadTempFile(result);
                  });
      });

      $("#btn-query").click(function () {
          _$table.bootstrapTable('refresh');
      });

        //制作BasicColors事件,用于请求变化后，刷新表格信息
      abp.event.on('app.createOrEditColorModalSaved', function () {
          _$table.bootstrapTable('refresh');
      });

      var oTable = new TableInit();
      oTable.Init();
      _$table.bootstrapTable('hideColumn', 'Id');
    });

})();
